package com.school.service.impl;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.school.dao.HljtDeviceDao;
import com.school.dao.HljtOtherInterfacesDao;
import com.school.entity.HljtDevice;
import com.school.entity.HljtDevicetask;
import com.school.entity.HljtSetting;
import com.school.entity.HljtVersion;
import com.school.entity.android.WithTheNew;
import com.school.entity.web.DeviceClassInfo;
import com.school.entity.web.HljtDeviceManagement;
import com.school.service.HljtOtherInterfacesService;
import com.school.util.WhetherNotToUpdate;

@Service
public class HljtOtherInterfacesServiceImpl implements HljtOtherInterfacesService{
	@Autowired
	private HljtOtherInterfacesDao hljtOtherInterfacesDao;
	@Autowired
	private HljtDeviceDao hljtDeviceDao;
	
	
	@Override
	@Transactional(rollbackFor=Exception.class)
	public Map<String, Object> withTheNew(HljtDevice hljtDevice , String type) {
		WithTheNew list = null;
		HljtDevice temp = hljtDeviceDao.equipmentRegistration(hljtDevice);
		List<HljtDeviceManagement>updatelist = new ArrayList<>();
		Map<String, Object> map = new HashMap<>();
		if(null !=type && "0".equals(type)) {
			//0是第一次请求要给数据
			WhetherNotToUpdate.setPattern(true);
		}
		if(null !=temp) {
			if(null!=temp.getUpdate()&&2==temp.getUpdate()) {
				WhetherNotToUpdate.setVersionUpdate(true);
			}
				
			if(null!=temp.getPattern()&&2==temp.getPattern()){
				WhetherNotToUpdate.setPattern(true);
				HljtDeviceManagement  f = new HljtDeviceManagement();
				f.setMac(hljtDevice.getMac());
				f.setPattern(1);
				updatelist.add(f);
			}
			if(null!=temp.getSwitchgear()&&2==temp.getSwitchgear()){
				WhetherNotToUpdate.setSwitchgear(true);
				HljtDeviceManagement  f = new HljtDeviceManagement();
				f.setMac(hljtDevice.getMac());
				f.setSwitchgear(1);
				updatelist.add(f);
			}
			if(null != temp.getState() && temp.getState()==1) {
				HljtDeviceManagement  f = new HljtDeviceManagement();
				f.setMac(hljtDevice.getMac());
				f.setState(2);
				updatelist.add(f);
			}
			if(null != updatelist && updatelist.size()>0) {
				hljtDeviceDao.updateDeviceByMac(updatelist);
			}
			Date Date1 =temp.getEndtime();
			Date Date2 =new  Date();
			//此时间要大于前台的轮循时间 ， 占时定在2分钟    2000*60<Date2.getTime() - Date1.getTime()
			
			if(2000*60<Date2.getTime() - Date1.getTime() || WhetherNotToUpdate.oneFalse()) {
				list = hljtOtherInterfacesDao.withTheNew(hljtDevice);
				list.setClassName(list.getGrade()+"("+list.getClasses()+")");
			//	list.setPattern(2000*60<Date2.getTime() - Date1.getTime()? true:WhetherNotToUpdate.isPattern());
				list.setPattern(WhetherNotToUpdate.isPattern());
				list.setSwitChgear(WhetherNotToUpdate.isSwitchgear());
				list.setVersionUpdate(WhetherNotToUpdate.isVersionUpdate());
				HljtDevicetask hljtDevicetask = new HljtDevicetask();
				hljtDevicetask.setClassid(list.getClassId());
				hljtDevicetask.setMac(hljtDevice.getMac());
				HljtDevicetask t =(HljtDevicetask) this.getTimeComparison(new Date(),hljtOtherInterfacesDao.findHljtDevicetaskByMac(hljtDevicetask)).get("modify");
				list.setPatternId(t.getPatternid());
				list.setEndTime(t.getEndtime());
				list.setStartTime(t.getStarttime());
				list.setPatternName(null);
				WhetherNotToUpdate.allFalse();
			}
		}
		map.put("hljtDevice", temp);
		map.put("withTheNew", list);
		return map;
	}
	@Override
	public HljtSetting switchTime() {
		HljtSetting hljtSetting = hljtOtherInterfacesDao.switchTime();
		return hljtSetting;
	}
	@Transactional(rollbackFor=Exception.class)
	@Override
	public void updateDeviceAllModify(HljtDevice hljtDevice) {
		if(null !=hljtDevice) {
			hljtDeviceDao.updateDeviceAllModify(hljtDevice);
		}
	}
	@Transactional(rollbackFor=Exception.class)
	@Override
	public void updateDeviceOneModify(List<HljtDeviceManagement>updatelist) {
		if(null != updatelist && updatelist.size()>0) {
			hljtDeviceDao.updateDeviceByMac(updatelist);
		}
	}
	
	protected Map<String , Object> getTimeComparison(Date date ,List<HljtDevicetask> dev ) {
		SimpleDateFormat s = new SimpleDateFormat("HH:mm");
		String stringDate= s.format(date);
		String replaceDate = stringDate.replaceAll(":","");
		int intDate = Integer.parseInt(replaceDate);
		int intStartTime = 0;
		int intEndTime = 0;
		Map<String , Object> map = new HashMap<>();
		if(null != dev && dev.size()>0) {
			for (HljtDevicetask hljtDevicetask : dev) {
				String replaceStartTime = hljtDevicetask.getStarttime().replaceAll(":","");
				intStartTime = Integer.parseInt(replaceStartTime);
				String replaceEndTime = hljtDevicetask.getEndtime().replaceAll(":","");
				intEndTime = Integer.parseInt(replaceEndTime);
				if(intDate>intStartTime && intDate<intEndTime) {
					map.put("isModify", true);
					map.put("modify", hljtDevicetask);
					return map;
				}
			}
		}
		HljtDevicetask hljtDevicetask = new HljtDevicetask();
		hljtDevicetask.setPatternid(1);
		hljtDevicetask.setEndtime("00:00");
		hljtDevicetask.setStarttime("00:00");
		map.put("isModify", false);
		map.put("modify", hljtDevicetask);
		return map;
	}
	@Transactional(rollbackFor=Exception.class)
	@Override
	public boolean addHljtVersion(HljtVersion hljtVersion) {
		HljtVersion temp = hljtOtherInterfacesDao.latestVersion();
		if(null != temp) {
			if(Integer.parseInt(hljtVersion.getVersionCode())>Integer.parseInt(temp.getVersionCode())) {
				hljtOtherInterfacesDao.addHljtVersion(hljtVersion);
				if(null!=hljtVersion.getId()) {
					return true;
				}
			}
		}else {
			hljtOtherInterfacesDao.addHljtVersion(hljtVersion);
			if(null!=hljtVersion.getId()) {
				return true;
			}
		}
		return false;
	}
	@Override
	public Map<String , Object> deviceUpgradeInfo(Integer pageSize , Integer pageIndex ) {
		List<DeviceClassInfo> list = hljtOtherInterfacesDao.findAllDevice();
		List<DeviceClassInfo> latestVersion = new ArrayList<>();
		List<DeviceClassInfo> noLatestVersion = new ArrayList<>();
		Map<String , Object> map = new HashMap<>();
		if(null != list && list.size()>0) {
			HljtVersion ver = hljtOtherInterfacesDao.latestVersion();
			if( null !=ver) {
				map.put("newVersionName",ver.getVersionName());
				map.put("newversioncode",ver.getVersionCode());
				for (DeviceClassInfo hljtDevice : list) {
					if(ver.getVersionCode().equals(hljtDevice.getVersioncode())) {
						latestVersion.add(hljtDevice);
					}else {
						noLatestVersion.add(hljtDevice);
					}
				}
			}else {
				latestVersion=list;
			}
		}
		map.put("latestVersion",this.getObjectPage(latestVersion , pageSize , pageIndex));
		map.put("noLatestVersion",this.getObjectPage(noLatestVersion , pageSize , pageIndex));
		map.put("latestVersionTotal",latestVersion.size());
		map.put("noLatestVersionTotal",noLatestVersion.size());
		
		return map;
	}
	
	protected List<DeviceClassInfo> getObjectPage(List<DeviceClassInfo> temp , Integer pageSize , Integer pageIndex ){
		List<DeviceClassInfo>re = new ArrayList<>();
		if(null != temp && temp.size()>0) {
			if(pageSize>=temp.size()) {
				re = temp;
			}else {
				Integer pageNum = (pageIndex-1)*pageSize;
				if(pageNum+pageSize>=temp.size()) {
					re = temp.subList(pageNum, temp.size());
				}else {
					re = temp.subList(pageNum, pageNum+pageSize);
				}
			}
		}
		return re;
	}
	@Transactional(rollbackFor=Exception.class)
	@Override
	public boolean needupgradeDevice(List<HljtDeviceManagement> list) {
		if(null != list && list.size()>0) {
			Integer temp = hljtDeviceDao.updateDeviceByMac(list);
			if(temp==list.size()) {
				return true;
			}
		}
		return false;
	}
	@Transactional(readOnly=true)
	@Override
	public String findDeviceByClassId(Integer classId) {
		return hljtDeviceDao.findDeviceByClassId(classId);
	}
	@Override
	public void modifyByCalssId(Integer calssid) {
			String mac = this.findDeviceByClassId(calssid);
			List<HljtDeviceManagement>updatelist = new ArrayList<>();
			HljtDeviceManagement  f = new HljtDeviceManagement();
			f.setMac(mac);
			f.setPattern(2);
			updatelist.add(f);
			this.updateDeviceOneModify(updatelist);
	}

}
